package com.hairline.hairlinemarket.controller;

import com.github.pagehelper.PageInfo;
import com.hairline.hairlinemarket.bean.MarketAddress;
import com.hairline.hairlinemarket.bean.MarketUser;
import com.hairline.hairlinemarket.bean.common.BaseRespVo;
import com.hairline.hairlinemarket.service.AddressService;
import javafx.scene.AmbientLight;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Author:杨小凯
 * Date:2023-04-22 15:40
 * Description:<描述>
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @RequestMapping("/list")
    public BaseRespVo list() {
        // 获取信息
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            // 拿到了个人id信息
            Integer userId = principal.getId();
            // 根据个人信息查询地址
            PageInfo pageInfo = addressService.selectAddressByUserId(userId);

            return BaseRespVo.ok(pageInfo);
        }
        return BaseRespVo.badArgument();
    }

    @RequestMapping("/save")
    public BaseRespVo save(@RequestBody MarketAddress marketAddress) {

        Integer data = addressService.addAddress(marketAddress);

        return BaseRespVo.ok(data);
    }

    @RequestMapping("/detail")
    public BaseRespVo detail(Integer id){
        // 查询信息回显
        MarketAddress address = addressService.selectPrimkey(id);

        return BaseRespVo.ok(address);
    }


    @RequestMapping("/delete")
    public BaseRespVo delete(@RequestBody Map map){

        Integer id = (Integer) map.get("id");
        // 查询信息回显
        addressService.deleteByPrimkey(id);

        return BaseRespVo.ok();
    }
}
